/**
 * JNI Interface Request List
 */

#include <assert.h>
#include <stdlib.h>

#include "disksim_jni_interface_list.h" 


struct request_list *request_list_create() 
{
	struct request_list *list = malloc(sizeof(struct request_list));
	assert(list != NULL);
	list->head = NULL;	
	list->tail = NULL;
	list->length = 0;
	return list;
}

void request_list_add(struct request_list *list, long long request_id, double request_timestamp) 
{	
	
	struct request_node *request  = malloc(sizeof(struct request_node));
	
	assert(request != NULL);
	
	request->request_id = request_id;
	request->request_timestamp = request_timestamp;
	request->next = NULL;
	
	if (list->length == 0) {
		list->head = request;
		list->tail = request;
	} else {
		list->tail->next = request;
		list->tail = request;
	}
	
	list->length++;	

}

void request_list_destroy(struct request_list *list) 
{
	struct request_node *request = list->head;

	while (request != NULL ) {
		struct request_node *temp = request->next;
		free(request);
		request = temp;
	}

	free(list);
}

void request_list_print(struct request_list *list) 
{
	struct request_node *request = list->head;

	while (request != NULL ) {
		
		printf("Request: %ll \t Finish: %f \n",request->request_id,request->request_timestamp);
  
		request = request->next;
	}

}

long long * request_list_to_array(struct request_list *list) 
{
	struct request_node *request;
	long long * requests = malloc(sizeof(long long) * list->length * 2);
	int i = 0;

	for (request = list->head; request != NULL; request = request->next,i+=2) {
		requests[i] = request->request_id;
		requests[i+1] = (long long) ((request->request_timestamp) * 1e6);
	}
	
	return requests;
}

